# -*- coding : utf_8 -*-
"""
=======================================
Time : 2025/4/10 11:19
Author : 田霄汉
Email : 522989570@qq.com
File : matplotlib_class08.py
Project : pandas_demo
Function : 对比柱状图
=======================================
"""
from config.setting import file_path
import pandas as pd
import matplotlib.pyplot as plt

excel08_path = file_path['excel_path'] + '/excel08.xlsx'
products = pd.read_excel(io=excel08_path,
                         skiprows=3,
                         usecols='C:G',
                         index_col='ID')
# 对数据排序,ascending=True是从小到大，False则是相反
products.sort_values(by='2024YEAR', inplace=True, ascending=False)

# 做对比柱形图
products.plot.bar(x='BOOK_NUM', y=['2023YEAR', '2024YEAR'], color=['blue', 'red'])
# 设置tital
plt.title('International Books by Compare of 2023&2024', fontsize=16, fontweight='bold')
# 设置x轴和y轴
plt.xlabel(xlabel='BOOK_NUM', fontweight='bold')
plt.ylabel(ylabel='PRICE', fontweight='bold')

# 设置x轴标题旋转，柱形图分成两部分：轴和figure，所以如果要设置x轴需要先获取轴部分
ax = plt.gca()
ax.set_xticklabels(products['BOOK_NUM'], rotation=45, ha='right')
# 在设置figure部分，将x轴和y轴的边框缩小一点
f = plt.gcf()
f.subplots_adjust(left=0.2, bottom=0.42)

# 紧凑型布局
plt.tight_layout()
plt.show()
